<?php
declare (strict_types=1);

namespace app\circle\logic;

use app\circle\model\Comment as CommentModel;
use app\common\exception\LogicException;
use app\common\logic\Common;
use app\user\event\subscribes\user\Circle as CircleEvent;
use Exception;
use think\facade\Event;

/**
 * 圈子评论逻辑层
 */
class Comment extends CommentModel
{

    /**
     * 评论圈子或回复评论
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-05-14
     *
     * @param array $param
     * @return void [type] [description]
     * @throws Exception
     */
    public function createComment(array $param)
    {
        // 验证数据
        $this->getValidate()->scene('create')->failException()->check($param);
        // 参数
        $param['user_id'] = request()->mid;
        $param['attach']  = isset($param['attach']) ? array_map(function ($value) {
            return intval($value);
        }, explode(',', $param['attach'])) : null;
        // 内容过滤
        $param['content'] = app(Common::class)->filterSensitive($param['content'], $param['circle_id'], 'circle');

        $this->startTrans();
        try {
            $create = $this::create($param);

            if ($create->isEmpty()) throw new LogicException("评论失败");

            $orignal              = app(Circle::class)->findOrEmpty($param['circle_id']);
            $orignal->comment_num += 1;
            if (!$orignal->save()) throw new LogicException("评论失败");
            // 事务处理消息 - 收到提问提醒
            $data['send_user_id']   = $param['user_id'];
            $data['recive_user_id'] = $param['reply_user_id'] ?? $orignal->user_id;
            $data['notify_data_id'] = $param['comment_id'] ?? $orignal->id;
            $data['content']        = $param['content'] ?? ($param['attach'] ? '图片消息' : '');
            $data['notify_type']    = isset($param['comment_id']) ? 5 : 4;
            Event::subscribe(CircleEvent::class);
            Event::trigger('CircleNotify', $data);

            $this->commit();
        } catch (LogicException|Exception $e) {
            $this->rollback();
            throw new LogicException($e->getMessage());
        }
    }

    /**
     * 删除评论
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-06-17
     */
    public function deleteComment(array $param)
    {
        // 验证数据
        $this->getValidate()->scene('delete')->failException()->check($param);
        // 参数
        $mid = request()->mid;
        $id  = intval($param['id']);

        $data = $this->with(['circle_user'])->findOrFail($id);
        if ($data->circle_user_id != $mid && $data->user_id != $mid) throw new LogicException("无删除权限");
        $circle = app(Circle::class)->findOrEmpty($data->circle_id);

        $this->startTrans();
        try {
            $circle->comment_num -= 1;
            if (!$circle->save() || !$data->delete()) throw new LogicException("删除失败");
            $this->commit();
        } catch (LogicException $e) {
            $this->rollback();
            throw new LogicException($e->getMessage());
        }
    }

    /**
     * 根据圈子ID获取评论
     * Author 亓官雨树<lucky.max@foxmail.com>
     * Date 2021-05-14
     *
     * @param int $circle_id [description]
     * @return mixed [type]            [description]
     */
    public function getCircleCommentById(int $circle_id)
    {
        return $this->with(['user_base', 'reply_user_name'])
            ->withCount(['like' => 'is_like'])
            ->whereCircleId($circle_id)
            ->order('create_time DESC')
            ->paginate($this->getListRows());
    }

}
